//获取应用实例
const app = getApp();

Page({
  /**
  * 页面的初始数据
  */
  data: {
    currtab: 0,
    swipertab: [{ name: '全部', index: 0, num: 3 }, { name: '已付款', index: 1, num: 0 }, { name: '已收货', index: 2, num: 0 }
      , { name: '已失效', index: 3, num: 1 }],
    orderList1:[],
    orderList1isNull:false,
    orderList2:[],
    orderList2isNull:false,
    orderList3: [],
    orderList3isNull: false,
    orderList4: [],
    orderList4isNull: false,
    orderList5: [],
    orderList5isNull: false,
  },

  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    // this.orderShow();
  },
  /**
  * 生命周期函数--监听页面初次渲染完成
  */
  onReady: function () {
    // 页面渲染完成
    this.getDeviceInfo();
  },

  onShow:function(){
    var that=this;
    console.log("onShow")
  },

  getDeviceInfo: function () {
    let that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          deviceW: res.windowWidth,
          deviceH: res.windowHeight
        })
      }
    })
  },

  /**
  * @Explain：选项卡点击切换
  */
  tabSwitch: function (e) {
    var that = this
    if (this.data.currtab === e.target.dataset.current) {
      return false
    } else {
      that.setData({
        currtab: e.target.dataset.current
      })
    }
  },

  tabChange: function (e) {
    // console.log(e.detail.current )
    this.setData({ currtab: e.detail.current })
    // this.orderShow();
    // this.statistics();
  },

  orderShow: function () {
    let that = this
    switch (this.data.currtab) {
      case 0:
        // that.getOrder1()
        break
      case 1:
        // that.getOrder2()
        break
      case 2:
        // that.getOrder3()
        break
      case 3:
        // that.getOrder4()
        break
    }
  },


//待付款
  getOrder1: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that=this;
    wx.request({
      url: app.globalData.api + '/app.php?c=PddOrder&a=getOrderList',
      data: {
        token: app.globalData.token,
        p: 1,
        per: 50,
        // order_status: '1',
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        that.data.orderList1=[];
        if (res.data.data.list.length==0){
          console.log("noData")
          that.setData({
            orderList1isNull: true
          })
        }else{
          for (var i = 0; i < res.data.data.list.length; i++) {
            if (res.data.data.list[i].detail.length > 0) {
              // if (res.data.data.list[i].detail[0].status == 1) {
                res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
                  that.setData({
                    orderList1: that.data.orderList1.concat(res.data.data.list[i]),
                  })
              // } 
            }
          }
        }
        if (that.data.orderList1.length > 0) {
          that.setData({
            orderList1isNull: false
          })
        } else {
          that.setData({
            orderList1isNull: true
          })
        }
        that.statistics();
      },fail(){
        wx.hideLoading();
      }
    })
    
  },

//待发货
  getOrder2: function () {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    that.data.orderList2 = [];
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getList',
      data: {
        token: app.globalData.token,
        status: '2',
        p:1,
        per:50,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.hideLoading();
        console.log(res)
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].detail.length > 0) {
            // if (res.data.data.list[i].detail[0].status == 2) {
              res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
              that.setData({
                orderList2: that.data.orderList2.concat(res.data.data.list[i])
              })
            // }
          }
        }
        if (that.data.orderList2.length > 0) {
          that.setData({
            orderList2isNull: false
          })
        } else {
          that.setData({
            orderList2isNull: true
          })
        }
        that.statistics();
      }, fail() {
        wx.hideLoading();
      }
    })
   
  },


//待收货
  getOrder3: function () {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    that.data.orderList3 = [];
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getList',
      data: {
        token: app.globalData.token,
        status:'3',
        p: 1,
        per: 50,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.hideLoading();
        // console.log(res)
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].detail.length > 0) {
            // if (res.data.data.list[i].detail[0].status == 3) {
              res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
              that.setData({
                orderList3: that.data.orderList3.concat(res.data.data.list[i])
              })
            // }
          }
        }
        if (that.data.orderList3.length > 0) {
          that.setData({
            orderList3isNull: false
          })
        } else {
          that.setData({
            orderList3isNull: true
          })
        }
        that.statistics();
      }, fail() {
        wx.hideLoading();
      }
    })
  },

  //待评价
  getOrder4: function () {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    that.data.orderList4 = [];
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getList',
      data: {
        token: app.globalData.token,
        p: 1,
        per: 50,
        status: '4'
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        wx.hideLoading();
        for (var i = 0; i < res.data.data.list.length; i++) {
          // console.log(res.data.data.list[i].detail.length)
          if (res.data.data.list[i].detail.length > 0) {
            // if (res.data.data.list[i].detail[0].status == 4) {
              res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
              that.setData({
                orderList4: that.data.orderList4.concat(res.data.data.list[i])
              })
              
            // }
          }
        }
        console.log(that.data.orderList4)
        if (that.data.orderList4.length > 0) {
          that.setData({
            orderList4isNull: false
          })
        } else {
          that.setData({
            orderList4isNull: true
          })
        }
        that.statistics();
      }, fail() {
        wx.hideLoading();
      }
    })
  },

  //退款退货
  getOrder5: function () {
    wx.showLoading({
      title: '加载中',
      mask: false
    })
    var that = this;
    that.data.orderList5 = [];
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getList',
      data: {
        token: app.globalData.token,
        p: 1,
        per: 50,
        status: '6'
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        wx.hideLoading();
        console.log(res)
        for (var i = 0; i < res.data.data.list.length; i++) {
          if (res.data.data.list[i].detail.length > 0) {
            // if (res.data.data.list[i].detail[0].status == 6) {
              res.data.data.list[i].detail[0].img = app.globalData.imgUrl + "" + res.data.data.list[i].detail[0].img;
              that.setData({
                orderList5: that.data.orderList5.concat(res.data.data.list[i])
              })
            // }
          }
        }
        if (that.data.orderList5.length > 0) {
          that.setData({
            orderList5isNull: false
          })
        } else {
          that.setData({
            orderList5isNull: true
          })
        }
        that.statistics();
      }, fail() {
        wx.hideLoading();
      }
    })
  },

//退货
  backgoods:function(e){
    // console.log(e);
    wx.navigateTo({
      url: 'setting-refund?order_detail_id='+e.currentTarget.dataset.id,
    })
  },

  //取消订单
  cancelOrder:function(e){
    // console.log(e)
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/cancel',
      data: {
        token: app.globalData.token,
        order_id:''+e.currentTarget.dataset.id,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          that.getOrder1();
        }else{
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
      }
    })
  },

  //订单统计
  statistics: function () {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/statistics',
      data: {
        token: app.globalData.token,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        // console.log(res)
        if (res.data.code == 1) {
          var num_a = "swipertab[" + 0 + "].num";//先用一个变量，把(swipertab[0].num)用字符串拼接起来  //待付款
          var num_b = "swipertab[" + 1 + "].num";//先用一个变量，把(swipertab[0].num)用字符串拼接起来  //待发货
          var num_c = "swipertab[" + 2 + "].num";//先用一个变量，把(swipertab[0].num)用字符串拼接起来  //待收货
          var num_d = "swipertab[" + 3 + "].num";//先用一个变量，把(swipertab[0].num)用字符串拼接起来  //待评价
          var num_e = "swipertab[" + 4 + "].num";//先用一个变量，把(swipertab[0].num)用字符串拼接起来  //退货退款
          that.setData({
            [num_a]: res.data.data.num1,
            [num_b]: res.data.data.num2,
            [num_c]: res.data.data.num3,
            [num_d]: res.data.data.num4,
            [num_e]: res.data.data.num_refund,
          })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }
        wx.hideLoading();
      }
    })
  },

  //获取支付表单
  getPayForm(e) {
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/getPayForm',
      data:
      {
        token: app.globalData.token,
        order_id: e.currentTarget.dataset.id,
        pay_method: '' + + app.globalData.payfrom,
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 1) {
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1000
          })
          that.orderShow();
          // that.statistics();
          // "{"appid":"wx48126316bdbec25b","noncestr":"lk8qhri54a7stxryu88e4n5tuhgc0f7f","package":"Sign = WXPay","partnerid":"1498191702","prepayid":"wx23131631764983da394a941c1058073000","sign_type":"HMAC - SHA256","timestamp":1590210991,"sign":"F9B89B33AC715AABE0C69AC825E1104BA283335731A01C1C7200C13420E65FFF"}"

          // var tempJson = JSON.parse(res.data.data.pay_parameters)
          // console.log(tempJson)
          // console.log(tempJson.appid)
          // var packages = "prepay_id=" + tempJson.prepayid
          // console.log(packages)
          // wx.requestPayment(
          //   {
          //     'timeStamp': '' +tempJson.timestamp,
          //     'nonceStr': '' + tempJson.noncestr,
          //     'package': "" + packages,
          //     'signType': 'MD5',
          //     'paySign': '' + tempJson.sign,
          //     'success': function (res) {
          //       console.log(res)
          //     },
          //     'fail': function (res) {
          //       console.log(res)
          //      },
          //     'complete': function (res) {
          //       console.log(res)
          //     }
          //   })
        } else {
          wx.showToast({
            title: '' + res.data.msg,
            icon: 'none',
            duration: 1000
          })
        }

      }, fail: function () {

      }
    });
  },

//延迟确认收货
  delayReceiving:function(e){
    var detail_id = e.currentTarget.dataset.id
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/delayReceiving',
      data: {
        token: app.globalData.token,
        order_detail_id: detail_id
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          that.getOrder3();
        }
        wx.showToast({
          title: '' + res.data.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  //确认收货
  orderConfirm:function(e){
    console.log(e.currentTarget.dataset.id)
    var detail_id = e.currentTarget.dataset.id
    console.log(app.globalData.token)
    console.log(detail_id)
    var that = this;
    wx.request({
      url: app.globalData.api + '/index.php/wap/Order/confirm',
      data: {
        token: app.globalData.token,
        order_detail_id: detail_id
      },
      headers: {
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success(res) {
        console.log(res)
        if(res.data.code==1){
          that.getOrder3();
        }
        wx.showToast({
          title: '' + res.data.msg,
          icon: 'none',
          duration: 1000
        })
      }
    })
  },

  comment:function(e){
    var id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: 'order-detail-3?order_id='+id,
    })
  },

  //待付款详情
  orderdetail: function (e) {
    // console.log(e)
    wx.navigateTo({
      url: 'order-detail-2?order_id=' + e.currentTarget.dataset.id,
    })
  },

  //待发货详情
  tobeShipped:function(e){
    var order_id=e.currentTarget.dataset.id
    // console.log(order_id)
    wx.navigateTo({
      url: '/pages/mine/order-detail-4?order_id=' + order_id,
    })
  },

  //待收货详情
  getGoods:function(e){
    var id=e.currentTarget.dataset.id
    // console.log(id)
    wx.navigateTo({
      url: 'order-detail-1?order_id='+id,
    })
  },

  /**
  * 生命周期函数--监听页面隐藏
  */
  onHide: function () {

  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {

  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {

  },

  /**
  * 用户点击右上角分享
  */
  onShareAppMessage: function () {

  }
});
